package stack;

import java.util.Stack;

public class Test {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '['){
                stack.push(ch);
            }
            else{
                //说明ch是右括号
                if(stack.empty()){
                    //右括号多
                    return false;
                }
                char tmp = stack.peek();
                if(tmp == '(' && ch == ')' ||
                        tmp == '{' && ch == '}' ||
                        tmp == '[' && ch == ']'){
                    //当前括号匹配
                    stack.pop();
                }
                else{
                    //左右括号不匹配
                    return false;
                }
            }
        }
        //判断是否为空
        if(stack.empty()){
            return true;
        }
        else{
            //左括号多
            return false;
        }
    }
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int count = 0;//计数：与压栈数据匹配则加一
        for(int i = 0; i < pushA.length; i++){
            if(pushA[i] == popA[count]){
                //先压栈，再判断
                stack.push(pushA[i]);
                while(stack.peek() == popA[count]){
                    stack.pop();
                    count++;
                    //popA遍历完说明正确
                    if(count == pushA.length){
                        return true;
                    }
                    /**
                     *弹出栈后，可能一个元素也不剩，这时再到while里判断，
                     *有可能能造成对空指针的解引用操作
                     */
                    if(stack.empty()){
                        break;
                    }
                }
            }
            else{
                stack.push(pushA[i]);
            }
        }

        return false;

    }
    class Solution {
        public int evalRPN(String[] tokens) {
            Stack<Integer> stack = new Stack<>();
            for(String str : tokens){
                if(judgment(str)){
                    int x = stack.pop();
                    int y = stack.pop();
                    switch(str){
                        case "+":
                            stack.push(y + x);
                            break;
                        case "-":
                            stack.push(y - x);
                            break;
                        case "*":
                            stack.push(y * x);
                            break;
                        case "/":
                            stack.push(y / x);
                            break;
                    }
                }
                else{
                    stack.push(Integer.parseInt(str));
                }
            }
            return stack.pop();
        }
        private boolean judgment(String str){
            if(str.equals("+") || str.equals("-") ||
                    str.equals("*") || str.equals("/")){
                return true;
            }
            else{
                return false;
            }
        }
    }
    public static void main(String[] args){
        Stack<Integer> stack = new Stack<>();

        System.out.println("=========================");
        MyStack mystack = new MyStack();
        mystack.push(10);
        mystack.push(11);
        mystack.push(12);
        mystack.push(13);
        mystack.pop();
        int ret = mystack.peek();
        System.out.println(ret);

    }
}
